       IDENTIFICATION DIVISION.
       PROGRAM-ID. TP2.
       AUTHOR. Grupo 5. 2do cuatrimestre 2011.

       ENVIRONMENT DIVISION.
         INPUT-OUTPUT SECTION.
           FILE-CONTROL.
             SELECT ASI-ARCH ASSIGN TO "ASIGNACIONES.DAT"
             ORGANIZATION IS LINE SEQUENTIAL.
             
             SELECT ORD-ARCH ASSIGN TO "SORT.TMP".

             SELECT ASIG-ARCH ASSIGN TO "ASIGNACIONES.IDX"
                 ORGANIZATION IS INDEXED
                 ACCESS MODE IS DYNAMIC
                 RECORD KEY IS ASIG-CLAVE
                 FILE STATUS IS ASIG-STATUS.

             SELECT RPT-ARCH ASSIGN TO "REPORTE.RPT"
             ORGANIZATION IS LINE SEQUENTIAL.

       DATA DIVISION.
         FILE SECTION.
           FD ASI-ARCH.
           01 ASI-REG.
             88 ASI-EOF                 VALUE HIGH-VALUES.
             02 ASI-TIPO-IDENT-CLI      PIC 9(01).
             02 ASI-IDENT-CLI-COD.
               03 ASI-COD-CLI           PIC X(06).
               03 ASI-FILLER            PIC 9(05).
             02 ASI-IDENT-CLI-CUIT REDEFINES ASI-IDENT-CLI-COD.
               03 ASI-CUIT-CLI          PIC 9(11).
             02 ASI-FECHA.
               03 ASI-ANIO              PIC 9(04).
               03 ASI-MES               PIC 9(02).
               03 ASI-DIA               PIC 9(02).
             02 ASI-PERFIL              PIC X(04).
             02 ASI-SENIORITY           PIC X(03).
             02 ASI-COD-EMPLE           PIC 9(05).
             02 ASI-IMPORTE             PIC 9(04)V99.

           COPY "Asignaciones-FD.cbl".

           SD ORD-ARCH.
           01 ORD-REG.
             88 ORD-EOF                 VALUE HIGH-VALUES.
             02 ORD-CLAVE.
               03 ORD-CUIT-CLI          PIC 9(11).
               03 ORD-FECHA.
                 04 ORD-ANIO            PIC X(04).
                 04 ORD-GUION1          PIC X(01) VALUE "-".
                 04 ORD-MES             PIC X(02).
                 04 ORD-GUION2          PIC X(01) VALUE "-".
                 04 ORD-DIA             PIC X(02).
               03 ORD-NRO-ASIG-CLI      PIC 9(03).
               03 ORD-PERFIL            PIC X(04).
               03 ORD-SENIORITY         PIC X(03).
               03 ORD-COD-EMPLE         PIC 9(05).
             02 ORD-IMPORTE             PIC 9(04)V99.
             02 ORD-RAZON-SOCIAL        PIC X(30).
             02 ORD-DOMICILIO           PIC X(30).
             02 ORD-PORC-DESC           PIC 9(02)V99.

           FD RPT-ARCH.
           01 RPT-REG                   PIC X(132).

         WORKING-STORAGE SECTION.
           01 ORD-CUIT-CLI-ANT          PIC 9(11).
           01 ORD-FECHA-ANT             PIC X(10).
           01 CLI-STATUS                PIC X(02).
           01 CLI-OPERACION             PIC 9(01).
           01 ASIG-STATUS               PIC X(02).
           01 NRO-ASIG-CLI              PIC 9(03).
           01 RAZON-SOCIAL              PIC X(30).
           01 DOMICILIO                 PIC X(30).
           01 PORC-DESC                 PIC 9(02)V99.
           
           01 TotalImporte              PIC 9(06)V99.
           01 TotalDescuento            PIC 9(06)V99.
           01 TotalImporteDescuento     PIC 9(06)V99.
           01 TotalImporteCliente       PIC 9(06)V99.
           01 Hoja                      PIC 9(03).
           01 FechaActual.
             02 AnioActual              PIC X(04).
             02 MesActual               PIC X(02).
             02 DiaActual               PIC X(02).
           
           COPY "Clientes-REG.cbl".
           
           01 Lineas                    PIC 9(02).

           01 RPT-ENC1.
             02 FILLER            PIC X(06) VALUE "Fecha ".
             02 RPT-ENC1-DIA      PIC X(02).
             02 FILLER            PIC X(01) VALUE "/".
             02 RPT-ENC1-MES      PIC X(02).
             02 FILLER            PIC X(01) VALUE "/".
             02 RPT-ENC1-ANIO     PIC X(04).
             02 FILLER            PIC X(47) VALUE SPACES.
             02 FILLER            PIC X(05) VALUE "Hoja ".
             02 RPT-ENC1-HOJA     PIC 9(03).
           
           01 RPT-ENC2.
             02 FILLER            PIC X(15) VALUE SPACES.
             02 FILLER            PIC X(08) VALUE "Listado ".
             02 FILLER            PIC X(10) VALUE "de factura".
             02 FILLER            PIC X(05) VALUE "cion ".
             02 FILLER            PIC X(11) VALUE "por cliente".
             02 FILLER            PIC X(08) VALUE " y fecha".

           01 RPT-ENC3.
             02 FILLER                PIC X(06) VALUE "CUIT: ".
             02 RPT-ENC3-CUIT-CLI     PIC 9(11).
             02 FILLER                PIC X(08) VALUE "  Razon ".
             02 FILLER                PIC X(08) VALUE "social: ".
             02 RPT-ENC3-RAZON-SOCIAL PIC X(30).

           01 RPT-ENC4.
             02 FILLER                PIC X(19) VALUE SPACES.
             02 FILLER                PIC X(10) VALUE "Domicilio:".
             02 FILLER                PIC X(04) VALUE SPACES.
             02 RPT-ENC4-DOMICILIO    PIC X(30).

           01 RPT-ENC5.
             02 FILLER                PIC X(07) VALUE "Fecha: ".
             02 RPT-ENC5-DIA          PIC X(02).
             02 FILLER                PIC X(01) VALUE "/".
             02 RPT-ENC5-MES          PIC X(02).
             02 FILLER                PIC X(01) VALUE "/".
             02 RPT-ENC5-ANIO         PIC X(04).

           01 RPT-TIT1.
             02 FILLER                PIC X(14) VALUE " |  NRO-VENTA ".
             02 FILLER                PIC X(11) VALUE " |  PERFIL ".
             02 FILLER                PIC X(14) VALUE " |  SENIORITY ".
             02 FILLER                PIC X(12) VALUE " |  COD.EMPL".
             02 FILLER                PIC X(05) VALUE "EADO ".
             02 FILLER                PIC X(14) VALUE " |  IMPORTE  |".

           01 RPT-TIT2.
             02 FILLER                PIC X(14) VALUE " -------------".
             02 FILLER                PIC X(14) VALUE "--------------".
             02 FILLER                PIC X(14) VALUE "--------------".
             02 FILLER                PIC X(14) VALUE "--------------".
             02 FILLER                PIC X(14) VALUE "--------------".

           01 RPT-LIN1.
             02 FILLER                PIC X(10) VALUE " |        ".
             02 RPT-LIN1-NRO-ASIG-CLI PIC 9(03).
             02 FILLER                PIC X(07) VALUE "  |    ".
             02 RPT-LIN1-PERFIL       PIC X(04).
             02 FILLER                PIC X(12) VALUE "  |         ".
             02 RPT-LIN1-SENIORITY    PIC X(03).
             02 FILLER                PIC X(11) VALUE " |         ".
             02 RPT-LIN1-COD-EMPLE    PIC 9(05).
             02 FILLER                PIC X(05) VALUE "  |  ".
             02 RPT-LIN1-IMPORTE      PIC ZZZZ.Z9.
             02 FILLER                PIC X(03) VALUE "  |".

           01 RPT-TOT1.
             02 FILLER                PIC X(41) VALUE SPACES.
             02 FILLER                PIC X(14) VALUE "Importe total:".
             02 FILLER                PIC X(07) VALUE "       ".
             02 RPT-TOT1-TotalImporte PIC ZZZZZZ.Z9.

           01 RPT-TOT2.
             02 FILLER                PIC X(13) VALUE SPACES.
             02 FILLER                PIC X(06) VALUE "Porc. ".
             02 FILLER                PIC X(10) VALUE "Descuento ".
             02 RPT-TOT2-PORC-DESC    PIC ZZ.Z9.
             02 FILLER                PIC X(07) VALUE "       ".
             02 FILLER                PIC X(14) VALUE "Importe desc.:".
             02 FILLER                PIC X(07) VALUE SPACES.
             02 RPT-TOT2-TotalDescuento PIC ZZZZZZ.Z9.

           01 RPT-TOT3.
             02 FILLER                PIC X(41) VALUE SPACES.
             02 FILLER                PIC X(12) VALUE "Importe con ".
             02 FILLER                PIC X(06) VALUE "desc.:".
             02 FILLER                PIC X(03) VALUE SPACES.
             02 RPT-TOT3-TotalImporteDescuento PIC ZZZZZZ.Z9.

           01 RPT-TOT4.
             02 FILLER                PIC X(10) VALUE "==========".
             02 FILLER                PIC X(10) VALUE "==========".
             02 FILLER                PIC X(10) VALUE "==========".
             02 FILLER                PIC X(10) VALUE "==========".
             02 FILLER                PIC X(10) VALUE "==========".
             02 FILLER                PIC X(10) VALUE "==========".
             02 FILLER                PIC X(11) VALUE "===========".
      
       PROCEDURE DIVISION.
         PERFORM Inicio

         PERFORM Procesar
          
         PERFORM Fin 
          
         STOP RUN
         .

       Inicio.
         OPEN INPUT ASI-ARCH
         MOVE 0 TO CLI-OPERACION
         CALL "ClientesLib"
             USING BY CONTENT CLI-OPERACION
                   BY REFERENCE CLI-REG, CLI-STATUS
         OPEN I-O ASIG-ARCH
         OPEN OUTPUT RPT-ARCH
         .

       Fin.
         CLOSE ASI-ARCH
         MOVE 3 TO CLI-OPERACION
         CALL "ClientesLib"
             USING BY CONTENT CLI-OPERACION
                   BY REFERENCE CLI-REG, CLI-STATUS
         CLOSE ASIG-ARCH
         CLOSE RPT-ARCH
         .

       Procesar.
         DISPLAY " "
         SORT ORD-ARCH ON ASCENDING KEY ORD-CLAVE
             INPUT PROCEDURE IS ProcesarEntrada
             OUTPUT PROCEDURE IS ProcesarSalida
         .

       ProcesarEntrada.
         PERFORM LeerAsignaciones
         
         PERFORM UNTIL ASI-EOF
             PERFORM ProcesarAsignaciones
             PERFORM LeerAsignaciones
         END-PERFORM
         .
       
       LeerAsignaciones.
         READ ASI-ARCH
             AT END SET ASI-EOF TO TRUE
         END-READ
         .
         
       ProcesarAsignaciones.
         PERFORM PasarDatosClientesAOrdenado
         PERFORM PasarFechaAOrdenado
         PERFORM BuscarProximoNroAsignacion
         
         MOVE NRO-ASIG-CLI  TO ORD-NRO-ASIG-CLI
         MOVE ASI-PERFIL    TO ORD-PERFIL
         MOVE ASI-SENIORITY TO ORD-SENIORITY
         MOVE ASI-COD-EMPLE TO ORD-COD-EMPLE
         MOVE ASI-IMPORTE   TO ORD-IMPORTE
         RELEASE ORD-REG
         .
         
       ProcesarSalida.
         PERFORM LeerOrdenado

         PERFORM UNTIL ORD-EOF
             MOVE ORD-CUIT-CLI TO ORD-CUIT-CLI-ANT
             
             PERFORM BuscarDatosCliente
             
             PERFORM UNTIL ORD-EOF
                 OR ORD-CUIT-CLI <> ORD-CUIT-CLI-ANT

                 MOVE ORD-FECHA TO ORD-FECHA-ANT
                 
                 MOVE 0 TO TotalImporteCliente
                 
                 PERFORM UNTIL ORD-EOF
                     OR ORD-CUIT-CLI <> ORD-CUIT-CLI-ANT
                     OR ORD-FECHA <> ORD-FECHA-ANT

                     PERFORM ListarEncabezadoReporte

                     MOVE 0 TO Lineas
                     PERFORM ListarEncabezadosColumnas
                     
                     MOVE 0 TO TotalImporte
                     
                     PERFORM UNTIL ORD-EOF
                         OR ORD-CUIT-CLI <> ORD-CUIT-CLI-ANT
                         OR ORD-FECHA <> ORD-FECHA-ANT
                         OR Lineas >= 60

                         PERFORM ListarPerfilSeniority
                         ADD 1 TO Lineas
                         
                         ADD ORD-IMPORTE TO TotalImporte

                         PERFORM LeerOrdenado
                     END-PERFORM
                     
                     PERFORM ListarTotales
                     ADD TotalImporte TO TotalImporteCliente
                     
                 END-PERFORM
                 
                 PERFORM ActualizarTotalAsignaciones
                 
             END-PERFORM
         END-PERFORM
         .
         
       LeerOrdenado.
         RETURN ORD-ARCH
             AT END SET ORD-EOF TO TRUE
         END-RETURN
         .

       ListarPerfilSeniority.
      *  DISPLAY " |        " ORD-NRO-ASIG-CLI " "
      *          " |    "    ORD-PERFIL       " "
      *          " |         "    ORD-SENIORITY   
      *          " |         "    ORD-COD-EMPLE    " "
      *          " |  "    ORD-IMPORTE      "  |"

         MOVE SPACES TO RPT-REG
         MOVE ORD-NRO-ASIG-CLI TO RPT-LIN1-NRO-ASIG-CLI
         MOVE ORD-PERFIL TO RPT-LIN1-PERFIL
         MOVE ORD-SENIORITY TO RPT-LIN1-SENIORITY
         MOVE ORD-COD-EMPLE TO RPT-LIN1-COD-EMPLE
         MOVE ORD-IMPORTE TO RPT-LIN1-IMPORTE
         MOVE RPT-LIN1 TO RPT-REG 
         WRITE RPT-REG
         .

       ListarEncabezadosColumnas.
      *  DISPLAY " |  NRO-VENTA "
      *          " |  PERFIL "
      *          " |  SENIORITY "
      *          " |  COD.EMPLEADO "
      *          " |  IMPORTE  |"

         MOVE SPACES TO RPT-REG
         MOVE RPT-TIT1 TO RPT-REG 
         WRITE RPT-REG

      *  DISPLAY " -------------------------------------------------"
      *          "--------------------"

         MOVE SPACES TO RPT-REG
         MOVE RPT-TIT2 TO RPT-REG 
         WRITE RPT-REG
         .

       ListarEncabezadoReporte.
      *  DISPLAY "================================================"
      *          "======================="
      
         MOVE SPACES TO RPT-REG
         MOVE RPT-TOT4 TO RPT-REG
         WRITE RPT-REG
         
         MOVE FUNCTION CURRENT-DATE(1:8) TO FechaActual
         ADD 1 TO Hoja
      *  DISPLAY "Fecha " DiaActual "/" MesActual "/" AnioActual 
      *  "                        "
      *           "                       Hoja " Hoja
         
         MOVE SPACES TO RPT-REG
         MOVE DiaActual TO RPT-ENC1-DIA   
         MOVE MesActual TO RPT-ENC1-MES
         MOVE AnioActual TO RPT-ENC1-ANIO
         MOVE Hoja TO RPT-ENC1-HOJA
         MOVE RPT-ENC1 TO RPT-REG 
         WRITE RPT-REG

      *  DISPLAY "              Listado de facturacion por cliente"
      *          " y fecha"

         MOVE SPACES TO RPT-REG
         MOVE RPT-ENC2 TO RPT-REG
         WRITE RPT-REG

      *  DISPLAY " "

         MOVE SPACES TO RPT-REG
         WRITE RPT-REG

      *  DISPLAY "CUIT: " ORD-CUIT-CLI
      *          "  Razon social: " RAZON-SOCIAL

         MOVE SPACES TO RPT-REG
         MOVE ORD-CUIT-CLI TO RPT-ENC3-CUIT-CLI
         MOVE RAZON-SOCIAL TO RPT-ENC3-RAZON-SOCIAL
         MOVE RPT-ENC3 TO RPT-REG
         WRITE RPT-REG
              
      *  DISPLAY "                   Domicilio:    " DOMICILIO
      
         MOVE SPACES TO RPT-REG
         MOVE DOMICILIO TO RPT-ENC4-DOMICILIO
         MOVE RPT-ENC4 TO RPT-REG
         WRITE RPT-REG
      
      *  DISPLAY "Fecha: " ORD-DIA "/" ORD-MES "/" ORD-ANIO

         MOVE SPACES TO RPT-REG
         MOVE ORD-DIA TO RPT-ENC5-DIA
         MOVE ORD-MES TO RPT-ENC5-MES
         MOVE ORD-ANIO TO RPT-ENC5-ANIO
         MOVE RPT-ENC5 TO RPT-REG
         WRITE RPT-REG

      *  DISPLAY " "

         MOVE SPACES TO RPT-REG
         WRITE RPT-REG
         .

       BuscarDatosCliente.
         IF ORD-RAZON-SOCIAL = SPACES
             MOVE 2 TO CLI-OPERACION
             MOVE ORD-CUIT-CLI TO CLI-CUIT
             CALL "ClientesLib"
                 USING BY CONTENT CLI-OPERACION
                       BY REFERENCE CLI-REG, CLI-STATUS

             MOVE CLI-RAZON-SOCIAL TO RAZON-SOCIAL
             MOVE CLI-DOMICILIO TO DOMICILIO
             MOVE CLI-PORC-DESC TO PORC-DESC
         ELSE
             MOVE ORD-RAZON-SOCIAL TO RAZON-SOCIAL
             MOVE ORD-DOMICILIO TO DOMICILIO
             MOVE ORD-PORC-DESC TO PORC-DESC
         END-IF
         .

       ListarTotales.
      *  DISPLAY " "

         MOVE SPACES TO RPT-REG
         WRITE RPT-REG

      *  DISPLAY "                                         " 
      *          "Importe total:       " TotalImporte

         MOVE SPACES TO RPT-REG
         MOVE TotalImporte TO RPT-TOT1-TotalImporte
         MOVE RPT-TOT1 TO RPT-REG
         WRITE RPT-REG

         COMPUTE TotalDescuento = TotalImporte * PORC-DESC / 100
         
      *  DISPLAY "             Porc. Descuento " PORC-DESC
      *          "       "
      *          "Importe desc.:       " TotalDescuento

         MOVE SPACES TO RPT-REG
         MOVE PORC-DESC TO RPT-TOT2-PORC-DESC
         MOVE TotalDescuento TO RPT-TOT2-TotalDescuento
         MOVE RPT-TOT2 TO RPT-REG
         WRITE RPT-REG

         COMPUTE TotalImporteDescuento = 
                 TotalImporte - TotalDescuento
      *  DISPLAY "                                         " 
      *          "Importe con desc.:   " TotalImporteDescuento

         MOVE SPACES TO RPT-REG
         MOVE TotalImporteDescuento TO RPT-TOT3-TotalImporteDescuento
         MOVE RPT-TOT3 TO RPT-REG
         WRITE RPT-REG

      *  DISPLAY " "

         MOVE SPACES TO RPT-REG
         WRITE RPT-REG
         
      *  DISPLAY "================================================"
      *          "======================="
      
         MOVE SPACES TO RPT-REG
         MOVE RPT-TOT4 TO RPT-REG
         WRITE RPT-REG
         .

       ActualizarTotalAsignaciones.
         MOVE ORD-CUIT-CLI-ANT TO ASIG-CUIT-CLI
         MOVE ORD-FECHA-ANT    TO ASIG-FECHA
         MOVE 999              TO ASIG-NRO-ASIG-CLI
         READ ASIG-ARCH

         IF ASIG-STATUS = "00"
             ADD ASIG-IMPORTE TO TotalImporteCliente
             MOVE TotalImporteCliente TO ASIG-IMPORTE
             REWRITE ASIG-REG
         ELSE
             IF ASIG-STATUS = "23"
                 MOVE ORD-CUIT-CLI-ANT    TO ASIG-CUIT-CLI
                 MOVE ORD-FECHA-ANT       TO ASIG-FECHA
                 MOVE 999                 TO ASIG-NRO-ASIG-CLI
                 MOVE TotalImporteCliente TO ASIG-IMPORTE
                 WRITE ASIG-REG
             END-IF
         END-IF
         .

       PasarDatosClientesAOrdenado.
         IF ASI-TIPO-IDENT-CLI = 1
             MOVE 1 TO CLI-OPERACION
             MOVE ASI-COD-CLI TO CLI-COD-CLI
             CALL "ClientesLib"
                 USING BY CONTENT CLI-OPERACION
                       BY REFERENCE CLI-REG, CLI-STATUS

             MOVE CLI-CUIT         TO ORD-CUIT-CLI
             MOVE CLI-RAZON-SOCIAL TO ORD-RAZON-SOCIAL
             MOVE CLI-DOMICILIO    TO ORD-DOMICILIO
             MOVE CLI-PORC-DESC    TO ORD-PORC-DESC
         ELSE
             MOVE ASI-CUIT-CLI     TO ORD-CUIT-CLI
             MOVE SPACES           TO ORD-RAZON-SOCIAL
             MOVE SPACES           TO ORD-DOMICILIO
             MOVE ZEROES           TO ORD-PORC-DESC
         END-IF
         .

       BuscarProximoNroAsignacion.
         MOVE 0 TO NRO-ASIG-CLI
         
         MOVE ORD-CUIT-CLI TO ASIG-CUIT-CLI
         MOVE ORD-FECHA    TO ASIG-FECHA
         MOVE 1            TO ASIG-NRO-ASIG-CLI
         READ ASIG-ARCH
             
         PERFORM UNTIL ASIG-EOF
             OR ASIG-STATUS <> "00"
             OR ASIG-CUIT-CLI <> ORD-CUIT-CLI
             OR ASIG-FECHA <> ORD-FECHA
             OR ASIG-NRO-ASIG-CLI = 999

             MOVE ASIG-NRO-ASIG-CLI TO NRO-ASIG-CLI

             READ ASIG-ARCH NEXT RECORD
                 AT END SET ASIG-EOF TO TRUE
             END-READ
         END-PERFORM
         
         ADD 1 TO NRO-ASIG-CLI
         
         MOVE ORD-CUIT-CLI TO ASIG-CUIT-CLI
         MOVE ORD-FECHA    TO ASIG-FECHA
         MOVE NRO-ASIG-CLI TO ASIG-NRO-ASIG-CLI
         MOVE ASI-IMPORTE  TO ASIG-IMPORTE
         WRITE ASIG-REG
         .

       PasarFechaAOrdenado.
         MOVE ASI-ANIO TO ORD-ANIO
         MOVE "-"      TO ORD-GUION1
         MOVE ASI-MES  TO ORD-MES
         MOVE "-"      TO ORD-GUION2
         MOVE ASI-DIA  TO ORD-DIA
         .
